#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/select.h>
#include "common.h"
#include "server.h"

int login(PK_REQUEST *rq,int sock)
{
	int count=0;
	int ret=-1;
	int i;
	char p[50];
	PK_COMMON pack;
	PK_ACK    ack;
	char temp[20];
	char tmp[16];
	while(count<3)
	{
		memset(&pack,0,sizeof(pack));
		memset(&p,0,sizeof(p));
		printf("username:");
		scanf("%s",temp);
		printf("password:");
		scanf("%s",tmp);
		strcpy(rq->username,temp);
		strcpy(rq->passwd,tmp);
		rq->opt_code=1;
		pack.opt_code=rq->opt_code;
		strcat(p,rq->username);
		strcat(p,",");
		strcat(p,rq->passwd);
		strcpy(pack.datas,p);

		/*printf("%s\n",rq->username);
		printf("%s\n",rq->passwd);
		printf("%s\n",pack.datas);
		printf("start write\n");*/

		ret=write(sock,&pack,sizeof(pack));
		//printf("end write\n");
		if(ret<0)
		{
			perror("error request - \n");
			return 0;
		}
		//printf("start read\n");
		ret=read(sock,&pack,sizeof(struct pk_common));
		//printf("end read\n");
		if(ret<0)
		{
			perror("error reply - \n");
			return 0;
		}
		ack.opt_code=pack.opt_code;
		//printf("%d\n",ack.opt_code);
		ack.number=((int)pack.datas[0])-48;
		//printf("%d\n",ack.number);
		if(ack.number==1) break;
		else count++;
	}
	if(count>=3) return -1;
	return 1;
}

ssize_t readline(int fd,void *buf,int size)
{
	char *strtmp=NULL;
	ssize_t reval;
	ssize_t realcount=0;
	strtmp=(char *)buf;
	
	while(size>0)
	{
		reval=read(fd,strtmp,1);
		if(reval<0)
		{
			if(errno == EINTR)
				continue;
			else
				return -1;
		}
		else if(reval == 0)
			break;
		else
		{
			realcount++;
			size--;
			if(*strtmp == '\n')
				break;
			strtmp++;
		}
	}
	*strtmp='\0';
	return realcount;
}

ssize_t writeline(int fd,const void *buf,int size)
{
	char *strtmp=NULL;
	ssize_t reval;
	ssize_t realcount=0;
	strtmp=(char *)buf;
	while(size>0)
	{
		reval=write(fd,strtmp,1);
		if(reval<0)
		{
			if(errno == EINTR)
				continue;
			else
				return -1;
		}
		else if(reval==0)
			break;
		else
		{
			realcount++;
			size--;
			strtmp++;
		}
	}
	write(fd,"\n",sizeof("\n"));
	return realcount;
}

int upack_package(const PK_COMMON *cm_pack,void *package)
{
	PK_REQUEST *request;
	PK_DATA *data;
	PK_ACK *ack;
	PK_ERROR *error;
	PK_IFLIVE *iflive;
	switch(cm_pack->opt_code)
	{
		case 1:
			
			request=(PK_REQUEST *)cm_pack;
			package=(void *)request;
			break;
		case 2:
			
			data=(PK_DATA *)cm_pack;
			package=(void *)data;
			break;
		case 3:
			
			ack=(PK_ACK *)cm_pack;
			package=(void *)ack;
			break;
		case 4:
			
			error=(PK_ERROR *)cm_pack;
			package=(void *)error;
			break;
		case 5:
			
			iflive=(PK_IFLIVE *)cm_pack;
			package=(void *)iflive;
			break;
		default:
			break;
	}
	return cm_pack->opt_code;
}

int live_confirm(int socketfd,const PK_IFLIVE *live,int pack_size)
{
	int ret=-1;
	ret=write(socketfd,(struct pk_common*)live,pack_size);
	if(ret<0)
		return -1;
	return 0;
}
